package mobiledev.project3.spaceinvaders;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Parcel;
import android.os.Parcelable;

public  class Sprite implements Parcelable {
	private Bitmap bitmap;
	protected Speed speed = new Speed();
	protected int x;
	protected int y;
	private boolean touched;
	
	public Sprite( Bitmap bitmap, int x, int y ) {
		this.setBitmap(bitmap);
		this.setX(x);
		this.setY(y);
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public Bitmap getBitmap() {
		return bitmap;
	}

	public void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}

	public boolean isTouched() {
		return touched;
	}

	public void setTouched(boolean touched) {
		this.touched = touched;
	}
	
	public void draw(Canvas canvas) {
		canvas.drawBitmap(bitmap, x - (bitmap.getWidth() / 2), y - (bitmap.getHeight() / 2), null);
	}

	public void handleActionDown(int eventX, int eventY) {
		if ( eventX >= ( x - bitmap.getWidth() / 2 ) && ( eventX <= ( x + bitmap.getWidth() / 2 ) ) ) {
			if ( eventY >= ( y - bitmap.getHeight() / 2 ) && ( y <= ( y + bitmap.getHeight() / 2 ) ) ) {
				// droid touched
				setTouched( true );
			} else {
				setTouched( false );
			}
		} else {
			setTouched( false );
		}
	}

	public Speed getSpeed() {
		return speed;
	}

	public void setSpeed(Speed speed) {
		this.speed = speed;
	}
	
	public void update() {
		if( !touched ) {
			x += (speed.getXv() * speed.getxDirection() );
		}
	}

	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void writeToParcel(Parcel arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}
}
